package logic

import (
	"baidu/modil/mysql"
	"context"

	"baidu/baidurpc/baidurpc"
	"baidu/baidurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookListLogic {
	return &BookListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookListLogic) BookList(in *baidurpc.BookListRequest) (*baidurpc.BookListResponse, error) {
	// todo: add your logic here and delete this line
	i := (in.Page - 1) * in.Size
	var book []mysql.Book
	mysql.DB.Debug().Offset(int(i)).Limit(int(in.Size)).Order("id desc").Find(&book)
	var bookList []*baidurpc.BookList
	for _, v := range book {
		list := baidurpc.BookList{
			Title:  v.Title,
			Text:   v.Text,
			UserId: v.UserId,
			Types:  v.Types,
		}
		bookList = append(bookList, &list)
	}

	return &baidurpc.BookListResponse{
		List: bookList,
	}, nil
}
